package com.wuge.gulimall.product.aop;

import com.google.common.collect.Maps;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect

@Component
public class RedisAspect {
    @Autowired
    RedissonClient redissonClient;
    @Around("@annotation(RedisQuery)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
//        解决缓存雪崩
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock(joinPoint.getSignature().getName().substring(6,13) );
        readWriteLock.readLock().lock();
        Object proceed = joinPoint.proceed();
       readWriteLock.readLock().unlock();

        return proceed;

    }
    @Around("@annotation(RedisUpdate)")
    public Object aroundUpdate(ProceedingJoinPoint joinPoint) throws Throwable {
//        解决缓存雪崩
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock(joinPoint.getSignature().getName().substring(3,10) + joinPoint.getArgs()[0]);
        readWriteLock.writeLock().lock();
        Object proceed = joinPoint.proceed();
        readWriteLock.writeLock().unlock();

        return proceed;

    }
}
